﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Command;
using System.Data;
using Structure.Interfaces;

namespace Application.Controls
{
    public partial class UserList : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                // добавляем кнопки удаления          
                DataControlField DeleteButtons = new ButtonField();
                (DeleteButtons as ButtonField).CommandName = "DeleteRow";
                (DeleteButtons as ButtonField).ButtonType = ButtonType.Image;
                (DeleteButtons as ButtonField).ImageUrl = "~/images/delete.png";
                UserGridView.Columns.Add(DeleteButtons);

               
            }

            // параметры таблицы
            UserGridView.AllowSorting = true;

            // получаем список проектов
            // iCommand _command;
            // _command = BuildCommand.Build();

            UserGridView.DataSource = BuildCommand.Build().UserList();
            UserGridView.DataBind();
        }

        protected void UserGridView_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            try
            {
                if (e.CommandName == "DeleteRow")
                {
                    int index = Convert.ToInt32(e.CommandArgument);

                    DataTable dt = UserGridView.DataSource as DataTable;

                    string id_task = dt.Rows[index]["#"].ToString();

                    BuildCommand.Build().DeleteUser(id_task);

                    dt.Rows.RemoveAt(index);

                    UserGridView.DataSource = dt;
                    UserGridView.DataBind();
                }

            }
            catch (Exception ex)
            { }
        }

        public void Update()
        {
            iCommand _command = BuildCommand.Build();
            UserGridView.DataSource = _command.UserList();
            UserGridView.DataBind();
        }

        protected void UserGridView_RowDeleted(object sender, GridViewDeletedEventArgs e)
        {
            
        } 
    }
}